package com.ruoyi.system.service.impl;

import com.ruoyi.system.service.CommonOpenApiService;
import com.ruoyi.system.wx.login.WxLoginService;
import com.ruoyi.system.wx.login.domain.WxLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Set;

import static com.ruoyi.common.constant.Constants.WORK_TYPE_PREFIX;

/**
 * @ClassName CommonOpenApiServiceImpl
 * @Description
 * @Author sizegang
 * @Date2025/4/19 21:45
 **/
@Component
public class CommonOpenApiServiceImpl implements CommonOpenApiService {


    @Autowired
    private WxLoginService wxLoginService;
    public static final String USER_USER_PREFIX = "ghb:%s";


    @Autowired
    private RedisTemplate redisTemplate;

    public Set<String> queryUserWorkType(String openId) {
        return redisTemplate.opsForSet().members(WORK_TYPE_PREFIX + openId);
    }

    public String queryOpenId(WxLoginVo wxLoginVo) {
        String firstCode = wxLoginVo.getFirstCode();

        String openId = wxLoginService.getOpenId(firstCode, wxLoginVo.getPlatform());

        Assert.notNull(openId, "openId is null");
        return openId;
    }


    public String getPhoneNumber(WxLoginVo wxLoginVo) {
        String secondCode = wxLoginVo.getSecondCode();

        String accessToken = wxLoginService.getAccessToken(wxLoginVo.getPlatform());

        String phoneNumber = wxLoginService.getPhoneNumber(secondCode, accessToken);
        return phoneNumber;
    }
}
